package com.fc.mall.portal.controller;

import com.fc.mall.base.object.Result;
import com.fc.mall.portal.domain.ConfirmOrderResult;
import com.fc.mall.portal.domain.GroupBuyProduct;
import com.fc.mall.portal.domain.ImmediatelyGroupOrderParam;
import com.fc.mall.portal.dto.OmsGroupCartItem;
import com.fc.mall.portal.dto.OmsOrderGroupBuyDto;
import com.fc.mall.portal.dto.OrderGroupParam;
import com.fc.mall.portal.service.GroupProductService;
import com.fc.mall.portal.service.OmsOrderGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by sunny on 2019/8/19.
 */
@Api(tags = "OmsOrderGroupBuyController",description = "团购订单管理")
@RestController
@RequestMapping("/group-order")
public class OmsOrderGroupBuyController {

    @Autowired
    private OmsOrderGroupService omsOrderGroupService;

    @Autowired
    private GroupProductService groupProductService;
    /**
     *
     * @return
     */
    @ApiOperation("创建团购认单信息")
    @PostMapping
    public Result createConfirmGroupOrder(@RequestBody OmsGroupCartItem omsCartItem)
    {
        ConfirmOrderResult confirmOrderResult = omsOrderGroupService.generateConfirmGroupOrder(omsCartItem);
        return Result.success(confirmOrderResult);
    }

    @ApiOperation("创建正式订单")
    @PostMapping("/immediately")
    public Result createGroupOrder(@RequestBody ImmediatelyGroupOrderParam immediatelyOrderParam)
    {
        return omsOrderGroupService.generateGroupOrder(immediatelyOrderParam);
    }

   @ApiOperation("查看拼团")
   @GetMapping("{groupId}/orderGroupId/{orderGroupId}")
    public Result getGroupOrderById(@PathVariable("groupId") Long groupId, @PathVariable("orderGroupId") String orderGroupId)
   {
       GroupBuyProduct groupBuy = groupProductService.getGroupProductById(groupId);
       Long orderGroupIdLong;
       try
       {
           orderGroupIdLong = Long.valueOf(orderGroupId);
       }
       catch (Exception e)
       {
           orderGroupIdLong = 0L;
       }
       OmsOrderGroupBuyDto orderGroupBuy = omsOrderGroupService.getOwnerGroupOrder(groupId, orderGroupIdLong);
       Map<String, Object> result = new HashMap<>();
       result.put("groupBuy", groupBuy);
       result.put("orderGroupBuy", orderGroupBuy);
       return Result.success(result);
   }


   @GetMapping("list")
   public Result handleGetOrderGroupList(OrderGroupParam orderGroupParam)
   {
       List<OmsOrderGroupBuyDto> list = omsOrderGroupService.getOrderGroupList(orderGroupParam);
       return Result.success(list);
   }




}
